<script setup>
import { computed, ref } from 'vue'
import goDetail from '@/utils/goDetail'

const props = defineProps({
  products: Array
})
//定义一个用于记录当前显示的产品索引的响应式变量
const currIndex = ref(0)
//计算属性，生成产品列表，每个子数组最多包含四个产品
const productsList = computed(() => {
  const list = [[]]
  props.products.forEach(image => {
    if (list[list.length - 1].length === 4) {
      list.push([]) //当前子数组满4个产品时，创建新的子数组
    }
    if (list[list.length - 1].length < 4) {
      list[list.length - 1].push(image) //将产品加入当前子数组
    }
  })
  return list
})
//切换到前一个产品
const onPrev = () => {
  if (currIndex.value === 0) {
    currIndex.value = productsList.value.length - 1 //当前索引为0时，循环到最后一个子数组
  } else {
    currIndex.value = currIndex.value - 1 //否则索引减1
  }
}
//切换到下一个产品
const onNext = () => {
  if (currIndex.value === productsList.value.length - 1) {
    currIndex.value = 0 //当前索引为最后一个子数组时，循环到第一个子数组
  } else {
    currIndex.value = currIndex.value + 1 //否则索引加1
  }
}

const onIndicatorClick = index => {
  currIndex.value = index
}
</script>

<template>
  <div class="goods-carousel">
    <ul class="carousel-body">
      <li
        :class="currIndex === index ? 'carousel-item active' : 'carousel-item'"
        v-for="(list, index) in productsList"
        :key="index"
      >
        <div class="slider">
          <a v-for="(product, index) in list" :key="index" @click="goDetail(product.id)">
            <img v-lazyImg="product.picture" alt="" />
            <p class="name">{{ product.name }}</p>
            <p class="price">¥{{ product.price }}</p>
          </a>
        </div>
      </li>
    </ul>
    <a href="javascript:;" class="carousel-btn prev" @click="onPrev"
      ><el-icon><ArrowLeft /></el-icon
    ></a>
    <a href="javascript:;" class="carousel-btn next" @click="onNext"
      ><el-icon><ArrowRight /></el-icon
    ></a>
    <div class="carousel-indicator">
      <span
        v-for="(list, index) in productsList"
        :key="index"
        :class="currIndex === index && 'active'"
        @click="onIndicatorClick(index)"
      ></span>
    </div>
  </div>
</template>

<style scoped lang="scss">
.goods-carousel {
  width: 100%;
  height: 380px;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  ul {
    list-style: none;
  }
  .carousel-body {
    width: 100%;
    height: 100%;
  }
  .carousel-item {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
    transition: opacity 0.5s linear;
    &.active {
      opacity: 1;
      z-index: 1;
    }
  }
  .slider {
    display: flex;
    justify-content: space-around;
    padding: 0 40px;
    a {
      text-decoration: none;
      color: #333;
      outline: none;
      width: 240px;
      text-align: center;
      cursor: pointer;
      img {
        padding: 20px;
        width: 190px;
        height: 190px;
      }
      .name {
        font-size: 16px;
        color: #666;
        padding: 0 40px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
      .price {
        font-size: 16px;
        color: #cf4444;
        margin-top: 15px;
      }
    }
  }
  .carousel-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    position: absolute;
    z-index: 2;
    text-align: center;
    line-height: 44px;
    transition: all 0.5s;
    text-decoration: none;
    outline: none;
    top: 110px;
    opacity: 1;
    background: transparent;
    color: #ddd;
    &.prev {
      left: 20px;
    }
    &.next {
      right: 20px;
    }
    i {
      font-size: 30px;
    }
  }
  .carousel-indicator {
    position: absolute;
    left: 0;
    bottom: 30px;
    z-index: 2;
    width: 100%;
    text-align: center;
    span {
      display: inline-block;
      width: 12px;
      height: 12px;
      background: rgba(0, 0, 0, 0.2);
      border-radius: 50%;
      & ~ span {
        margin-left: 12px;
      }
      &.active {
        background: #27ba9b;
      }
    }
  }
}
</style>
